import "regenerator-runtime/runtime.js";
// import { createRequire } from "module";
// //@ts-ignore
// import mathsource from "mathjax-full/components/src/source.js";
// const { source } = mathsource;
// const require = createRequire(import.meta.url);
const version = "3.2.0";
const PACKAGES = "base, autoload, require, ams, newcommand";
const argv = {
    packages: PACKAGES,
    width: 80 * 16,
    ex: 8,
    em: 16,
    inline: false,
    // assistiveMml: false,
    dist: true,
    fontURL: `https://cdn.jsdelivr.net/npm/mathjax@${version}/es5/output/chtml/fonts/woff-v2`,
};
const config = {
    // options: {
    //     enableAssistiveMml: argv.assistiveMml,
    // },
    startup: { typeset: false },
    loader: {
        // source: argv.dist ? {} : source,
        require: require,
        load: [
            "input/asciimath",
            "output/chtml",
            "input/tex-full",
            "input/mml",
            "adaptors/liteDOM",
        ],
        paths: {
            mathjax: "mathjax-full/es5",
        },
    },
    tex: {
        packages: argv.packages.replace("*", PACKAGES).split(/\s*,\s*/),
    },
    chtml: {
        fontURL: argv.fontURL,
    },
};
function initialize() {
    global.MathJax = config;
    require("mathjax-full/" +
        (argv.dist ? "es5" : "components/src/startup") +
        "/startup.js");
}
initialize();
// /**
//  * @returns {string}
//  * */

// export async function getstylesheet() {
//     await MathJax.startup.promise;

//     const adaptor = MathJax.startup.adaptor;
//     const styletexts = adaptor.textContent(MathJax.chtmlStylesheet());
//     return styletexts;
// }
import { defaultopts } from "./rendermathpre";
const defopts = Object.assign(
    {
        display: !argv.inline,
        em: argv.em,
        ex: argv.ex,
        containerWidth: argv.width,
    },
    defaultopts
);
/**
 *
 * @param {string} code
 * @param {string} language
 * @param {object}opts
 *
 * @returns {Promise<{html:string,css:string}>}
 *
 */

export async function rendermath(code, language, opts = {}) {
    const options = Object.assign({}, defopts, opts);
    const input = code;
    await MathJax.startup.promise;
    const adaptor = MathJax.startup.adaptor;
    const rendermathname = Reflect.get(mathrenderfuns, language);
    if (!rendermathname) {
        throw new Error("mathtype:latex,asciimath,mathml");
    }
    const mathfun = Reflect.get(MathJax, rendermathname);
    const node = await Reflect.apply(mathfun, MathJax, [input, options]);

    const html = adaptor.outerHTML(node);

    // MathJax.startup.document.clear();
    // MathJax.startup.document.updateDocument();
    const styletexts = adaptor.textContent(MathJax.chtmlStylesheet());
    return { css: styletexts, html: html };
}
const mathrenderfuns = {
    latex: "tex2chtmlPromise",
    asciimath: "asciimath2chtmlPromise",
    mathml: "mathml2chtmlPromise",
};
